
package net.lwf.web.struts.action.maths;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import lwf.web.struts.action.constant.IActionConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.math.arithmatics.BasicMathProcessHandler;
import net.lwf.web.constant.IPageTitle;
import net.lwf.web.struts.action.LwfWorksheetAction;
import net.lwf.web.struts.action.LwfWorksheetActionForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class BasicMathAction extends LwfWorksheetAction {
	
	public ActionForward generate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BasicMathProcessHandler handler = (BasicMathProcessHandler) getHandler(request);
		QuestionInfo qinfo = handler.generateSingle();
		//Set the question data
		setQuestionData(request, qinfo);
		//Set form data
		LwfWorksheetActionForm aform = (LwfWorksheetActionForm) form;
		aform.setHtmlQuestion(qinfo.getHtmlQuestion());
		aform.setAskQuestion(true);
		aform.setQuestionNumber(getQuestionNumber(request));
		aform.setInstructionText(qinfo.getPlaceHolder().toString());
		return mapping.findForward("display");
	}
	
	@Override
	public String getPageTitle() {
		return IPageTitle.BASIC_MATHS;
	}

	@Override
	public String getMainTitle() {
		return "Basic Math";
	}
	
	@Override
	public String getPdfLink() {
		return null;
	}

	@Override
	public String getQuizLink() {
		return IActionConstant.BASICMATH_QUIZ_LINK;
	}
	
	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		return new BasicMathProcessHandler(getOperation());
	}
	
/*	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BasicMathActionForm bmaf = (BasicMathActionForm) form;
		bmaf.setAskQuestion(false);
		//bmaf.setOperations(getOperations());
		
		return mapping.findForward("success");
	} //home
	
//	private List<RadioData> getOperations() {
//		List<RadioData> operations = new ArrayList<RadioData>();
//		TopicInfo topicinfo = TabMenuServiceFactory.getInstance().getMenu(getGrade(), "Maths", IHandlerConstant.ARITHMATICS);
//		List<LinkInfo> links = topicinfo.getLinkInfo();
//		for(LinkInfo info: links) {
//			RadioData data = new RadioData(info.getName(), info.getName());
//			operations.add(data);
//		}
//		return operations;
//	}

	//public ActionForward selectoperation(ActionMapping mapping,
	public ActionForward generate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//String operation = request.getParameter("operation");
		//BasicMathProcessHandler sumdetails = new BasicMathProcessHandler(operation);
		BasicMathActionForm bmaf = (BasicMathActionForm) form;
		bmaf.setAskQuestion(true);
		BasicMathProcessHandler sumdetails = new BasicMathProcessHandler(getOperation());
		//set to session
		setSumData(request, sumdetails);
		// Write the HTML to response
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		out.println(sumdetails.getDisplayFormatter().getFormattedQuestion());
		//the html string value will be retrieved as 'req.responseText'
		// and will be displayed using the element id = questionblock
		out.flush();
		bmaf.setQuestionNumber(Integer.parseInt(sumdetails.getQuestionNum()));
		bmaf.setHtmlQuestion(sumdetails.generateQuestionTable());

		return mapping.findForward("success");
	}
	
	@Override
	public String getPageTitle() {
		return IPageTitle.BASIC_MATHS;
	}

	@Override
	public String getMainTitle() {
		return null;
	}

	@Override
	public String getPdfLink() {
		return null;
	}

	@Override
	public String getQuizLink() {
		return IActionConstant.BASICMATH_QUIZ_LINK;
	}*/
	
	
}
